package server;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import both.*;

public class RosterTab extends JPanel implements ActionListener {

    private Game teams;
    private GameShowServer srv;
    private JTable table;
    private DefaultTableModel change;
    private final JButton Unregister, OpenRegister;
    private boolean regOpen = false;

    private static String[] headers = {StringFormatter.formatString("Name", StringFormatter.TITLE_STR), StringFormatter.formatString("Team Name", StringFormatter.TITLE_STR)};

    public RosterTab(GameShowServer server) {
        super();
        teams = server.getGame();
        srv = server;
    	// this is will call method from the Roster class to fill up the table
        // with names
        String[][] names = {};
        change = new DefaultTableModel(names, headers);
        table = new JTable(change) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        JScrollPane sp = new JScrollPane(table);
        sp.setPreferredSize(new Dimension(700, 700));
        add(sp);

        Unregister = new JButton(StringFormatter.formatString("Unregister", StringFormatter.ANSWER_STR, 100));
        OpenRegister = new JButton(StringFormatter.formatString("Open Registration", StringFormatter.ANSWER_STR, 15));
        Unregister.setAlignmentY(CENTER_ALIGNMENT);
        Unregister.setActionCommand("Unregister");
        Unregister.addActionListener(this);
        OpenRegister.setActionCommand("Open Registration");
        OpenRegister.addActionListener(this);
        Unregister.setEnabled(false);
        // this listeners to see if anything is selected in the table
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent e) {
                int row = table.rowAtPoint(e.getPoint());
                int column = table.columnAtPoint(e.getPoint());
                if (row >= 0 && column >= 0) {
                    if (!regOpen) {
                        Unregister.setEnabled(true);
                    }
                }
            }
        });
        table.getTableHeader().setReorderingAllowed(false);
        add(Unregister);
        add(OpenRegister);

        server.addListener(this);

        setVisible(true);
    }

	// will add the team members and teamnames to the table, every time there is a
    // change made to the table
    // new-registration
    //update-registration
    public void populateList() {
        Roster rost = teams.getRoster();
        ArrayList<Contestant> contestants = rost.contestantsByTeamName();
        String[][] contents = new String[contestants.size()][2];
        for (int i = 0; i < contents.length; i++) {
            Contestant cont = contestants.get(i);
            contents[i][0] = cont.getName();
            contents[i][1] = cont.getTeamName();
        }
        change.setDataVector(contents, headers);
    }

    public void registrationEvent(ActionEvent ev) {
        Roster roster = teams.getRoster();
        Contestant contestant = (Contestant) ev.getSource();
        switch (ev.getActionCommand()) {
            case "update-registration":
                if (contestant != null) {
                    roster.removeContestant(roster.getContestantByID(contestant.getUniqueID()));
                }
            case "new-registration":
                roster.addContestant(contestant);
            default:
        }
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        String eventName = event.getActionCommand();
        // this case should check if something in the table is selected
        if (eventName.equals("update-registration") || eventName.equals("new-registration")) {
            registrationEvent(event);
            populateList();
        }
        if (eventName.equals("Unregister")) {
            if (regOpen) {
                JOptionPane.showMessageDialog(this,
                        StringFormatter.formatString("Close the registration before unregistering a contestant", StringFormatter.QUESTION_STR, WIDTH),StringFormatter.formatString("Can't Unregister", StringFormatter.QUESTION_STR, WIDTH / 2), WIDTH);
            } else {
                int row = table.getSelectedRow();
                Contestant cont = teams.getRoster().contestantsByTeamName().get(row);
                teams.getRoster().removeContestant(cont);
                populateList();
                GameShowServer.setContestants(teams.getRoster().contestantsByTeamName().size());
            }
        }
        if (eventName.equalsIgnoreCase("Open Registration")) {
            OpenRegister.setText(StringFormatter.formatString("Close Registration", StringFormatter.ANSWER_STR, 15));
            OpenRegister.setActionCommand("Close Registration");
            Unregister.setEnabled(false);
            regOpen = true;
            srv.startListening();
        }
        if (eventName.equalsIgnoreCase("Close Registration")) {
            OpenRegister.setText(StringFormatter.formatString("Open Registration", StringFormatter.ANSWER_STR, 15));
            OpenRegister.setActionCommand("Open Registration");
            Unregister.setEnabled(true);
            regOpen = false;
            srv.stopListening();
        }
    }
}
